package com.pj.viewpager.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pj.viewpager.R;
import com.pj.viewpager.bean.NewsListJavaBean;

import java.util.List;

public class MyNewsListAdapter extends BaseAdapter {

    List<NewsListJavaBean> data;
    private ImageView newsListItemImg;
    private TextView newsListItemTitle;
    private TextView newsListItemContent;

    public MyNewsListAdapter(List<NewsListJavaBean> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        @SuppressLint("ViewHolder") View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_list_item, null);
        initView(v);
        NewsListJavaBean bean = data.get(i);
        newsListItemImg.setImageResource(bean.getImageId());
        newsListItemTitle.setText(bean.getTitle());
        newsListItemContent.setText(bean.getContent());
        return v;
    }

    private void initView(View v) {
        newsListItemImg = (ImageView) v.findViewById(R.id.news_list_item_img);
        newsListItemTitle = (TextView) v.findViewById(R.id.news_list_item_title);
        newsListItemContent = (TextView) v.findViewById(R.id.news_list_item_content);
    }
}
